//
// Created by wenbo on 24-9-7.
//
#include "drv_mrc_HE.h"

uint16_t ADC_BUFFER[3] = {0, 0, 0};

HE_device MRC_HE_device;

void drv_mrc_HE_init(char const *dev_name, HE_device *device)
{
    device->dev_name = dev_name;

    device->MRC_Init = MRC_Init;
    device->MRC_Init("mrc", &device->MRC, &htim2, INA1_GPIO_Port, INA1_Pin,INB1_GPIO_Port, INB1_Pin,TIM_CHANNEL_1);
    device->MRC_lock = MRC_lock;
    device->MRC_unlock = MRC_unlock;
    device->MRC_switch = MRC_switch;
    device->MRC_set_voltage = MRC_set_voltage;
    device->VNH7070_Multisense_ADC_process = VNH7070_Multisense_ADC_process;
    device->ADC_Update_Flag = 0;

    drv_key_init(&device->key0,"key0", KEY0_GPIO_Port, KEY0_Pin);
    drv_key_init(&device->key1,"key1", KEY1_GPIO_Port, KEY1_Pin);

    drv_led_init(&device->led0,"led0", LED0_GPIO_Port, LED0_Pin,LOW_LEVEL);
    drv_led_init(&device->led1,"led1", LED1_GPIO_Port, LED1_Pin,LOW_LEVEL);

    led_off(&device->led0);
    led_off(&device->led1);

    HAL_ADC_Start_DMA(&hadc1,(uint32_t *)ADC_BUFFER,3);

    moving_average_create(&device->upper_sensor.filter,8,1);
    moving_average_create(&device->bottom_sensor.filter,8,1);
    moving_average_create(&device->mrc_sensor.filter,8,1);

}

void HE_adc_cal_filter(HE_device *device) {
    if(device->ADC_Update_Flag == 1) {
        device->upper_sensor.raw_data = (ADC_BUFFER[1])*3.3/4096;
        device->bottom_sensor.raw_data = (ADC_BUFFER[2]*3.3)/4096;
        device->mrc_sensor.raw_data = (ADC_BUFFER[0]*3.3)/4096;
        moving_average_filter(&device->upper_sensor.filter,device->upper_sensor.raw_data);
        moving_average_filter(&device->bottom_sensor.filter,device->bottom_sensor.raw_data);
        moving_average_filter(&device->mrc_sensor.filter,device->mrc_sensor.raw_data);
    }
    device->ADC_Update_Flag = 0;

}

